package com.rainbow.gwt.accounting.client.ui;

import java.util.Date;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.FocusEvent;
import com.google.gwt.event.dom.client.FocusHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.datepicker.client.DatePicker;
import com.rainbow.gwt.accounting.client.UserDescService;
import com.rainbow.gwt.accounting.client.UserDescServiceAsync;
import com.rainbow.gwt.accounting.client.entity.UserDesc;
import com.rainbow.gwt.accounting.client.util.DictionaryUtilService;
import com.rainbow.gwt.accounting.client.util.DictionaryUtilServiceAsync;

/**
 * @author Rainbow E-mail:jamerainbow@gmail.com
 * @version 1.0.0 Create Time:Nov 7, 2010
 * 
 */
public class UserInfoDemo {
	private VerticalPanel mainPanel = new VerticalPanel();
	private HorizontalPanel usernamePanel = new HorizontalPanel();
	private HorizontalPanel birthdayPanel = new HorizontalPanel();
	private HorizontalPanel sexPanel = new HorizontalPanel();
	private HorizontalPanel cellphonePanel = new HorizontalPanel();
	private HorizontalPanel addressPanel = new HorizontalPanel();
	private Label usernameLabel = new Label();
	private Label birthdayLabel = new Label();
	private Label sexLabel = new Label();
	private Label cellphoneLabel = new Label();
	private Label addressLabel = new Label();
	private TextBox usernameText = new TextBox();
	private TextBox birthdayText = new TextBox();
	private TextBox sexText = new TextBox();
	private TextBox cellphoneText = new TextBox();
	private ListBox addressList = new ListBox();
	private DatePicker datePicker = new DatePicker();
	private Button addButton = new Button("Add User");
	private FlexTable allUsersInfo = new FlexTable();
	private final UserDescServiceAsync userDescService = GWT
			.create(UserDescService.class);
	private final DictionaryUtilServiceAsync dictionaryService = GWT
			.create(DictionaryUtilService.class);

	public UserInfoDemo() {
		allUsersInfo.setText(0, 0, "Username");
		allUsersInfo.setText(0, 1, "Birthday");
		allUsersInfo.setText(0, 2, "Sex");
		allUsersInfo.setText(0, 3, "Cellphone");
		allUsersInfo.setText(0, 4, "Address");
		usernameLabel.setText("Username:");
		birthdayLabel.setText("Birthday:");
		birthdayText.setReadOnly(true);
		datePicker.setValue(new Date());
		datePicker.setVisible(false);
		sexLabel.setText("Sex:");
		cellphoneLabel.setText("Cellphone:");
		addressLabel.setText("Address:");
		dictionaryService.getCountries(new AsyncCallback<List<String>>() {

			public void onFailure(Throwable caught) {
				System.err.println("Faild to get countries!");
			}

			public void onSuccess(List<String> result) {
				List<String> countries = result;
				for (int i = 0; i < countries.size(); i++) {
					String country = countries.get(i);
					addressList.addItem(country, country);
				}
			}

		});

		usernamePanel.add(usernameLabel);
		usernamePanel.add(usernameText);
		birthdayPanel.add(birthdayLabel);
		birthdayPanel.add(birthdayText);
		birthdayPanel.add(datePicker);
		sexPanel.add(sexLabel);
		sexPanel.add(sexText);
		cellphonePanel.add(cellphoneLabel);
		cellphonePanel.add(cellphoneText);
		addressPanel.add(addressLabel);
		addressPanel.add(addressList);
		mainPanel.add(usernamePanel);
		mainPanel.add(birthdayPanel);
		mainPanel.add(sexPanel);
		mainPanel.add(cellphonePanel);
		mainPanel.add(addressPanel);
		mainPanel.add(addButton);
		datePicker.addValueChangeHandler(new ValueChangeHandler<Date>() {

			public void onValueChange(ValueChangeEvent<Date> event) {
				Date date = event.getValue();
				String dateString = DateTimeFormat.getFormat(
						PredefinedFormat.DATE_SHORT).format(date);
				birthdayText.setText(dateString);
				datePicker.setVisible(false);
			}
		});
		birthdayText.addFocusHandler(new FocusHandler() {

			public void onFocus(FocusEvent event) {
				datePicker.setVisible(true);
			}
		});

		addButton.addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent event) {
				addUser();
			}
		});
		refreshUsersPanel();
		RootPanel.get("userInfo").add(mainPanel);
		RootPanel.get("allUsers").add(allUsersInfo);
	}

	private void addUser() {
		UserDesc userDesc = new UserDesc();
		userDesc.setUsername(usernameText.getText());
		userDesc.setBirthday(birthdayText.getText());
		userDesc.setCellphone(cellphoneText.getText());
		userDesc.setSex(sexText.getText());
		userDesc.setAddress(addressList.getValue(addressList.getSelectedIndex()));
		userDescService.setUserDesc(userDesc, new AsyncCallback<Void>() {

			public void onSuccess(Void result) {
				refreshUsersPanel();
			}

			public void onFailure(Throwable caught) {
			}
		});
	}

	private void refreshUsersPanel() {
		userDescService.getUserDescs(new AsyncCallback<List<UserDesc>>() {

			public void onFailure(Throwable caught) {

			}

			public void onSuccess(List<UserDesc> result) {
				for (int i = 0; i < result.size(); i++) {
					UserDesc userDesc = result.get(i);
					allUsersInfo.setText(i + 1, 0, userDesc.getUsername());
					allUsersInfo.setText(i + 1, 1, userDesc.getBirthday());
					allUsersInfo.setText(i + 1, 2, userDesc.getSex());
					allUsersInfo.setText(i + 1, 3, userDesc.getCellphone());
					allUsersInfo.setText(i + 1, 4, userDesc.getAddress());
				}
			}

		});

	}
}
